package com.shanzmoo.admin.web.product;


import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.core.manager.ProductManager;
import com.shanzmoo.db.product.entity.*;
import com.shanzmoo.db.product.entity.dto.ProductSaveDto;
import com.shanzmoo.db.product.entity.vo.*;
import com.shanzmoo.db.product.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品spu 前端控制器
 * </p>
 *
 * @author Xueqz
 * @since 2020-12-09
 */
@Api(tags = "商品-商品spu")
@RestController
@RequestMapping("/api/product/spu")
public class ProductSpuController {

    @Autowired
    private ProductManager productManager;
    @Autowired
    private IProductSpuService productSpuService;
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private IProductBrandService productBrandService;
    @Autowired
    private IProductUnitService productUnitService;
    @Autowired
    private IProductAttributeService productAttributeService;

    @ApiOperation(value = "商品列表")
    @GetMapping("/getSpuList")
    public SHttpResponse<PageRespBase<ProductVO>> getSpuList(ProductQueryVO productQueryVO) {//@Valid @RequestBody
        SResponse<PageRespBase<ProductVO>> re = productManager.getSpuList(productQueryVO);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "商品上下架操作")
    @GetMapping("/operationSpu")
    public SHttpResponse<Boolean> operationSpu(Integer id,Boolean shelvesMark) {
        ProductSpuEntity productSpuEntity = new ProductSpuEntity();
        productSpuEntity.setId(id);
        productSpuEntity.setUpdateUser(CurrentUserCache.getUserId());
        productSpuEntity.setUpdateTime(LocalDateTime.now());
        productSpuEntity.setShelves(shelvesMark);
        productSpuService.updateById(productSpuEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }

    @ApiOperation(value = "商品保存操作")
    @PostMapping("/saveSpu")
    public SHttpResponse<Boolean> saveSpu(@Valid @RequestBody ProductSaveDto productSaveDto) {
        SResponse<Boolean> re = productManager.saveSpu(productSaveDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "商品更新操作")
    @PostMapping("/updateSpu")
    public SHttpResponse<Boolean> updateSpu(@Valid @RequestBody ProductSaveDto productSaveDto) {
        SResponse<Boolean> re = productManager.updateSpu(productSaveDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "获取商品发布常量对象集")
    @GetMapping("/getStaticProductData")
    public SHttpResponse<Map> getStaticProductData() {
        List<ProductBrandEntity> brandList = productBrandService.getBrandListMethod(new BrandQueryVO());
        List<ProductUnitEntity> unitList = productUnitService.getUnitListMethod(new UnitQueryVO());
        SResponse<List<CategoryVo>> catData = productCategoryService.getAllTree();
        PushAttributeVO pushAttributeVO = productAttributeService.getPushAttributeVoList();
        List<CategoryVo> catTree = catData.getData();
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("brandList",brandList);
        returnMap.put("unitList",unitList);
        returnMap.put("catTree",catTree);
        returnMap.put("pushAttributeVO",pushAttributeVO);
        return SHttpResponse.transform(SResponse.ok(returnMap));
    }

    @ApiOperation(value = "获取商品编辑/查看详情信息")
    @GetMapping("/getSpuDetailBySpuId")
    public SHttpResponse<ProductSaveDto> getSpuDetailBySpuId(Integer spuId) {
        SResponse<ProductSaveDto> rs = productManager.getSpuDetailBySpuId(spuId);
        return SHttpResponse.transform(rs);
    }


}
